import { createReactAgent } from '@langchain/langgraph/prebuilt';
import { ChatOpenAI } from '@langchain/openai';
import { McpServer } from './McpServer';
import { getAgent } from '../utils/model';

const config = {
  verbose: false, // 关闭详细日志以提高性能
  /**
   * 模型推导度
   */
  temperature: 0.7,

  /**
   * 模型最大token数
   */
  maxTokens: 8000, // 限制token数量以控制成本

  /**
   * 模型名称
   * @defaultValue 'gpt-3.5-turbo'
   */
  model: 'Qwen/QwQ-32B', //'deepseek-ai/DeepSeek-V3.1-Terminus',

  /**
   * 配置
   */
  configuration: {
    /**
     * 密钥
     */
    apiKey: 'sk-sssdueysiwnoinzwqvtwhbwzdniszusgvprzrcaavppoocnb',

    /**
       * 基础URL

       */
    baseURL: 'https://api.siliconflow.cn/v1/',
  },

  /**
   * 启用流式响应
   */
  streaming: true,
};

let QwenAgentInstance: ReturnType<typeof createReactAgent> | null = null;
export const getQwenAgentInstance = async () => {
  const Qwen = new ChatOpenAI({ ...config, model: 'Qwen/QwQ-32B' });

  if (!QwenAgentInstance) {
    const tools = await McpServer.getTools();
    QwenAgentInstance = createReactAgent({
      llm: Qwen,
      tools,
    });
  }

  return QwenAgentInstance;
};

let QwenImageAgentInstance: ReturnType<typeof createReactAgent> | null = null;
export const getQwenImageAgentInstance = () => {
  const QwenImage = new ChatOpenAI({
    ...config,
    model: 'Qwen/Qwen-Image',
  });

  if (!QwenImageAgentInstance) {
    QwenImageAgentInstance = createReactAgent({
      llm: QwenImage,
      tools: [],
    });
  }
  return QwenImageAgentInstance;
};
// 配置通义千问API
// const model = new ChatOpenAI({
//   modelName: 'qwen-max',
//   temperature: 0.7,
//   apiKey: 'sk-329081e00c0a481a9177ee626cbb2b18',
//   configuration: {
//     baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
//   },
// });

let AliAgentInstance: ReturnType<typeof createReactAgent> | null = null;
export const getAliAgent = () => {
  if (!AliAgentInstance) {
    const Ali = new ChatOpenAI({
      model: 'qwen-max',
      temperature: 0.7,
      apiKey: 'sk-329081e00c0a481a9177ee626cbb2b18',
      configuration: {
        baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
      },
    });
    AliAgentInstance = createReactAgent({
      llm: Ali,
      tools: [],
    });
  }
  return AliAgentInstance;
};
